package cn.xiaodi.site.jdbc.mapper.impl;

import cn.xiaodi.site.jdbc.mapper.RowMapper;
import cn.xiaodi.site.jdbc.model.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author xiao
 * @email 245938767@qq.com
 * @gitee https://gitee.com/liu_yong_qiee/
 * @date 2021/10/13 9:03 上午
 */
public class StudentRowMapper implements RowMapper<Student> {
    private JdbcTemplate jdbcTemplate;
    private DataSourceTransactionManager transactionManager;

    /**
     * 查询所有的信息
     */
    @Override
    public List<Student> findAll() {
        TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition());
        try {
            List<Student> studentsList = new ArrayList<Student>();
            // 查询的sql语句
            String sql = "select * from student";
            // 调用方法获取集合数据
            List stuList = jdbcTemplate.queryForList(sql);
            // 遍历集合，每一个student代表一个Student实体
            Iterator iterator = stuList.iterator();
            while (iterator.hasNext()) {
                Map map = (Map) iterator.next();
                Student stu = new Student();
                stu.setName((String) map.get("name"));
                stu.setId((Integer) map.get("id"));
                stu.setAge((Integer) map.get("age"));
                stu.setGender(((String) map.get("gender")));
                stu.setEmail((String) map.get("email"));
                studentsList.add(stu);

            }
            transactionManager.commit(status);
            return studentsList;

        } catch (Exception e) {
            e.printStackTrace();
            transactionManager.rollback(status);
            return null;
        }

    }

    /**
     * 添加的方法
     */
    @Override
    public void add(Student student) {
        String sql = "insert into student values(?,?,?)";
        Object[] args = new Object[] { student.getId(), student.getName(),
                student.getAge() };
        jdbcTemplate.update(sql, args);
    }

    /**
     * 删除
     * @return
     */
    @Override
    public int delete(Integer id) {
        String sql = "delete from student where id=?";
        int update = jdbcTemplate.update(sql, id);
        return update;
    }

    /**
     * 更新的方法
     */
    @Override
    public void update(Student student) {
        String sql = "update student set name=? where id=?";
        Object[] args = new Object[] { student.getName(), student.getId() };
        jdbcTemplate.update(sql, args);

    }

    /**
     *
     * 查询总记录数
     */
    @Override
    public Integer count() {
        String sql = "select count(1) from student";
        Object count = jdbcTemplate.queryForObject(sql, Object.class);
        return ((BigDecimal) count).intValue();
    }

    /**
     * 调用存储过程实现根据学生id查询学生姓名
     *
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    @Override
    public String byIdSelectName(Integer id) {
        String sql = "select name from student where id=?";
        Object[] args = new Object[] { id };
        String name = jdbcTemplate.queryForObject(sql, args,
                String.class);
        return name;
    }


    /**
     * 根据id查询单个对象
     */
    @Override
    public Student byIdStudent(Integer id) {
        String sql = "select * from student where id=?";
        Student student = jdbcTemplate.queryForObject(sql,
                new BeanPropertyRowMapper<Student>(Student.class), id);
        return student;
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }
    public void setTransactionManager(DataSourceTransactionManager transactionManager){this.transactionManager=transactionManager;}

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
